package com.hrt.manage.product.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;






<<<<<<< HEAD





import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.StaleObjectStateException;
=======
>>>>>>> refs/remotes/origin/master
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.ProductDetails;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.ResourceType;
import com.hrt.bean.Resources;
import com.hrt.manage.product.dao.ProInforDao;
import com.hrt.manage.product.dao.ProductDetailsDao;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.manage.product.service.ProInforService;
import com.hrt.manage.resource.service.ResourceService;

@Service("ProductInforService")
public class ProInforServiceImpl implements ProInforService {

	@Autowired
	private ProInforDao dao;

	@Autowired
	private ProDetailsService svr;
	
	
	
	
	@Autowired
	private ResourceService resService;

	public ProInforServiceImpl() {
		// TODO Auto-generated constructor stub
	}

	@Transactional
	@Override
	public void saveProduct(ProductInfor product) {
		// TODO Auto-generated method stub
		dao.saveEntity(product);
	}

	@Override
	public List<ProductInfor> queryByPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return dao.queryByPage(pageIndex, pageSize);
	}

	@Transactional
	@Override
	public void deleteById(Long[] proIds) {
		List<ProductInfor> list = dao.getByIds(proIds);

		for (ProductInfor product : list) {

			List<ProductDetails> details =svr.queryByProId(product.getId());
			if (details != null && details.size() > 0) {
				Long[] detailIds = new Long[details.size()];
				int i=0;
				for (Iterator<ProductDetails> iter=details.iterator();iter.hasNext();) {
					detailIds[i] = iter.next().getId();
					i++;
				}
				svr.deleteById(detailIds);
			}

			product.setIsdelete(new Integer(1));
			dao.updateEntity(product);
		}
	}

	@Override
	public ProductInfor getProductByid(Long proId) {
		// TODO Auto-generated method stub
		return dao.getById(proId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly=false)
	public void udateProduct(ProductInfor product) throws IllegalAccessException,StaleObjectStateException, InvocationTargetException {
		// TODO Auto-generated method stub
		try{
		dao.updateEntity(product);
		}catch(StaleObjectStateException ex){
			try {
			ProductInfor lastProduct=dao.getById(product.getId());
			int versionId=lastProduct.getVersion();
				BeanUtils.copyProperties(lastProduct, product);
				lastProduct.setVersion(versionId);
				dao.updateEntity(lastProduct);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				throw new IllegalAccessException("更新失败");
				//e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				throw new InvocationTargetException(e);
				//e.printStackTrace();
			}
		}
	}

	@Override
	public Long totalCount() {
		// TODO Auto-generated method stub
		return dao.totalCount();
	}

	@Override
	public List<ProductInfor> queryByProType(Long typeId) {
		// TODO Auto-generated method stub
		List<ProductInfor> list = new ArrayList<ProductInfor>();
		Map con = new HashMap();
		List conditions = new ArrayList();
		con.put("field", "typeId");
		con.put("value", typeId);
		conditions.add(con);

		list = dao.queryByCondition(conditions);
		return list;
	}

	@Override
	public List<ProductInfor> queryByProType(Long typeId, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		List conditions = new ArrayList();
		List<ProductInfor> list = new ArrayList<ProductInfor>();
		Map con = new HashMap();
		con.put("field", "typeId");
		con.put("value", typeId);
		conditions.add(con);
		list = dao.queryPageByCondition(conditions, pageIndex, pageSize);
		return list;
	}

	@Override
	public List<Resources> getProductResource(Long proId) {
		List<Resources> resources = new ArrayList<Resources>();
		resources=resService.getProductResource(proId, ResourceType.PRODUCT_RESOURCE);
		///resources = dao.getProductResource(proId, ResourceType.PRODUCT_RESOURCE);
		
		return resources;

	}

	@Override
	public List<Resources> getProductResource(Long proId, Integer resType,int pageIndex, int pageSize) {
		List<Resources> resources = new ArrayList<Resources>();
		resources=resService.getProductResource(proId, ResourceType.PRODUCT_RESOURCE,resType,pageIndex,pageSize);
		//resources = dao.getProductResource(proId,ResourceType.PRODUCT_RESOURCE, pageIndex, pageSize);
		return resources;

	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED,readOnly=false)
	public ProductInfor updateTotal(ProductInfor product) throws IllegalAccessException, StaleObjectStateException,InvocationTargetException{

		try{
			List<ProductDetails> details=svr.queryByProId(product.getId());
			product=dao.getById(product.getId());
			Long total=0l;
			if(details!=null && !details.isEmpty()){
			for(ProductDetails detail:details)
			{
				if(detail!=null){
					if(detail.getIsdelete().intValue()==0){
						total=total+detail.getTotal().longValue();
					}
				}
			}
			product.setTotal(total);
			dao.updateEntity(product);
			}
			return dao.getById(product.getId());
			
			}catch(StaleObjectStateException ex){
				List<ProductDetails> details=svr.queryByProId(product.getId());
				product=dao.getById(product.getId());
				Long total=0l;
				if(details!=null && !details.isEmpty()){
				for(ProductDetails detail:details)
				{
					if(detail!=null){
						if(detail.getIsdelete().intValue()==0){
							total=total+detail.getTotal().longValue();
						}
					}
				}
				product.setTotal(total);
				dao.updateEntity(product);
				}
				return dao.getById(product.getId());
			}
		
		
		
	}

	@Override
	public List<ProductInfor> queryProducts(String itemCondition) {
	
		List<ProductInfor> list = new ArrayList<ProductInfor>();
		
		
		list = dao.queryProducts(itemCondition);
		return list;
	}

	@Override
	public List<ProductInfor> listProduct() {
		List<ProductInfor> list = new ArrayList<ProductInfor>();
		list=dao.loadList(ProductInfor.class);
		return list;
	}

	/**
	 * 计算库存
	 * @param productInfor
	 * @return
	 */
	@Override
	public Long computeTotal(ProductInfor productInfor) {
		// TODO Auto-generated method stub
		List<ProductDetails> detail=svr.queryByProId(productInfor.getId());
		Long totalSum=0l;
		for(ProductDetails d:detail)
		{
			if(d!=null)
			{
				totalSum=totalSum+d.getTotal();
			}
		}
		productInfor.setTotal(totalSum);
		productInfor=dao.updateTotal(productInfor);
		return productInfor.getTotal();
	}

	@Override
	public List<Resources> getProductResource(Long proId, int pageIndex,
			int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
